<template>
	<view class="box">
	    <!-- 选择日期 -->
		<view class="selectMember">
			<view class="member">
				<text style="position: relative;">{{start_date || '选择日期：'}}
					<picker mode="date" :start="start" :end="end"  @change="bindDateChange" v-if="!readType" style="position: absolute;left:0;top: 0;width: 100%;height: 100%;">
						<view class="date"></view>
					</picker> 
				</text>
			</view>
			<view class="save" @click="showModalbtn(4)" v-if="!readType">保存</view>
			<view class="toSend" @click="showModalbtn(2)">申请评估</view>
		</view>
	
	    <view class="content">
	        <view class="question" v-for="(item,index) in psychologyQuestion" :key="index">
				<view class="title">
					<text>{{index <= 8 ? '0'+(index+1) : index+1}}</text>
					<view>{{item.name}}</view>
				</view>
				<view class="option" v-for="(aitem,aindex) in item.adTopicsRepliesEntityList" :key="aindex">
					<view @click="answer(aitem,item)"><text>{{aitem.elections}}、{{aitem.value}}</text> 
						<image src="@/static/img/select@2x.png" v-if="aitem.checked == false"></image>  
						<image src="@/static/img/selected@2x.png" v-else></image>
					</view>
				</view>
			</view>
	    </view>
		<u-modal v-model="showModal1" :show-cancel-button="true" @confirm="Modalconfirm" :content="showContent"></u-modal>
	</view>
</template>

<!-- src="@/static/js/monitor/accomplishment/index" -->
<script>
	import {getUrlParam} from '@/static/js/filter.js'
	export default {
		data () {
			const currentDate = this.getDate({
				format: true
			})
			return {
				psychologyQuestion:[],
				start:'',
				end:currentDate,
				start_date:'',
				showContent: '',
				showModal1: false,
				userId: '',
				healthId: '',
				readType: ''
			}
		},
		created() {
			const _this = this;
			_this.readType = getUrlParam('selected');
			_this.question().then((reponse) => {
				if(getUrlParam('id')) {
					const _this = this;
					_this.id = getUrlParam('id');		
					_this.$api.psychologyQuestionInfo(_this.id).then((res) => {
						if(res.data.returnCode == 200) {
							const data1 = res.data.result && res.data.result.newUserTopicsEntities;
							const data2 = res.data.result && res.data.result.newAppMentalHealthAssessmentEntity;
							_this.userId = data2.userId;
							_this.healthId = data2.healthId;
							_this.start_date = data2.fillFormDate;
							_this.editTid = data2.tid
							_this.psychologyQuestion.map((a)=>{
								data1.map((b)=>{ 
									if(a.id == b.titleId){
										a.adTopicsRepliesEntityList.map((c)=>{
											c.nid = b.id
											if(c.elections == b.userOption){
												c.checked = true
											}
										})
									}
								})
							})
						}
						
					})
				}
			})
		},
		methods:{
			//获取心理健康问卷
			question() {
				return new Promise((success,error)=>{
					this.$api.psychologyQuestion('ids=24').then((res)=>{
						if(res){
							this.psychologyQuestion = res.data.result[24] && res.data.result[24].adTopicsRepliesEntityList.map((a)=>{
								a.adTopicsRepliesEntityList = a.adTopicsRepliesEntityList.map((b)=>{b.checked = false;return b})
								return a
							})
							success(res.data.result)
						}else{
							error('')
						}
					})
				})
			},
			showModalbtn(e) {
				if(!this.start_date) {
					uni.showToast({
						title:'请选择日期',
						icon: 'none'
					})
					return 
				}
				
				//过滤选中的值
				const appUserTopicsEntities = this.psychologyQuestion.map((a) => {
					const bresult = a.adTopicsRepliesEntityList.filter((b) => {
						return b.checked
					})
					return {
						id:bresult.length > 0 && bresult[0].nid||a.adTopicsRepliesEntityList[0].nid,
						titleId: a.id,
						userOption:bresult.length > 0 ? bresult[0].elections : '',
						userValue: bresult.length > 0 ? bresult[0].value : ''
					}
				})
				
				const record = {
					appUserTopicsEntities: appUserTopicsEntities,
					appUserTopicsTitleEntity: {
						healthId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).healthId ||'',
						userId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId || '',
					},
					fillFormDate: this.start_date,
					source:1,
					status: e,
					tid: [24],
					upload:4,
					userId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).userId || '',
					healthId: localStorage.getItem('userInfo') && JSON.parse(localStorage.getItem('userInfo')).healthId ||'',
				}
				if(this.id) {
					record.id = Number(this.id);
					record.appUserTopicsTitleEntity.id = this.editTid
					this.$api.psychologyQuestionUpdate(record).then((res) => {
						uni.showToast({
							title: res.data.message,
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateTo({
								url:'/pages/healthStatus/navigation?current=3'
							})
						},2000)
					})
				}else {
					this.$api.psychologyQuestionAdd(record).then((res) => {
						if(res.data.returnCode == 200) {
							uni.showToast({
								title: res.data.message,
								icon: 'none'
							})
							setTimeout(() => {
								uni.navigateTo({
									url:'/pages/healthStatus/navigation?current=3'
								})
							},2000)
						}
					})
				}
			},
			//点击选中答案
			answer(aitem,item) {
				if(this.readType) {
					return
				}
				item.adTopicsRepliesEntityList.map((a) => {
					a.checked = false
					return a
				})
				aitem.checked = true
			},
			// 日期
			bindDateChange(e) { 
				this.start_date = e.target.value;
			},
			// 获取当前时间
			getDate(type) {
			   const date = new Date();
			   let year = date.getFullYear();
			   let month = date.getMonth() + 1;
			   let day = date.getDate();
			  
			   if (type === 'start') {
				year = year - 60;
			   } else if (type === 'end') {
				year = year + 2;
			   }
			   month = month > 9 ? month : '0' + month;;
			   day = day > 9 ? day : '0' + day;
			   return `${year}-${month}-${day}`;
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/css/monitor/accomplishment/index.scss';
</style>
